/**
 * Created by HUANGFR2 on 8/21/2016.
 */
angular.module('audit_merchant', []).controller('audit_merchantController', function ($scope, $http, $rootScope, $uibModal, MerchantService) {
    //console.log('audit_merchant init');
    $rootScope.active = 'am';
    $http.get('/merchant')
        .then(function (res) {
            var data = res.data;
            if (data.msg == 'SUCCESS') {
                $scope.merchants = data.merchants;
            } else {
                console.error('load merchant error : ' + data.error);
                alert('load merchant error : ' + data.error);
            }
        }, function (err) {
            console.error('load merchant error : ' + err);
            alert('load merchant error : ' + err);
        });

    $scope.updateStatus = function (merchant, status) {
        MerchantService.updateMerchantStatus(merchant, status);
    };

    $scope.openWarning = function (merchant) {
        var modalInstance = MerchantService.openWarningModal($uibModal, $scope, merchant);
    };
});

angular.module('audit_merchant').controller('low_gradeController', function ($scope, $http, $rootScope, $uibModal, MerchantService) {
    //console.log('low_grade init');
    $rootScope.active = 'lgm';
    $http.get('/merchant/low')
        .then(function (res) {
            var data = res.data;
            if (data.msg == 'SUCCESS') {
                $scope.merchants = data.merchants
            } else {
                alert('get low merchant error : ' + data.error);
            }
        }, function (err) {
            console.error('get low merchant error : ' + err);
        });

    $scope.updateStatus = function (merchant, status) {
        MerchantService.updateMerchantStatus(merchant, status);
    };

    $scope.openWarning = function (merchant) {
        var modalInstance = MerchantService.openWarningModal($uibModal, $scope, merchant);
    };
});

angular.module('audit_merchant').controller('merchantWarningModalController', function ($scope, $http, $uibModalInstance, parentScope, merchant) {

    $scope.merchant = merchant;
    $scope.confirm = function () {
        parentScope.updateStatus(merchant, 3);
        $scope.cancel();
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

});
